package com.xiaotu.spider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xiaotu.common.util.CacheHandler;
import com.xiaotu.common.util.GsonUtils;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

/**
 * @类名 APipeline
 * @日期 2017年2月16日
 * @作者 高海军
 * @功能 pipeline基类
 */
public abstract class APipeline implements Pipeline
{
    private static final Logger LOGGER = LoggerFactory.getLogger(APipeline.class);

    @Autowired
    protected CacheHandler cacheHandler;

    protected APageProcessor pageProcessor;

    @Override
    public void process(ResultItems resultItems, Task task)
    {
        try
        {
            if (resultItems == null || resultItems.getAll().isEmpty())
                return;
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("pipeline ResultItems:" + GsonUtils.toJson(resultItems.getAll()));
            this.doProcess(resultItems, task);
        }
        catch (Exception e)
        {
            LOGGER.error("[" + this.getSpiderType() + "] pipeline error:", e);
        }
    }

    /**
     * 已爬取数据的保存操作
     *
     * @param resultItems
     * @param task
     */
    protected abstract void doProcess(ResultItems resultItems, Task task);

    public void setPageProcessor(APageProcessor pageProcessor)
    {
        this.pageProcessor = pageProcessor;
    }

    protected String getSpiderType()
    {
        if (this.pageProcessor == null)
            return null;
        return pageProcessor.getSpiderType();
    }
}
